package com.example.smarthomebluetoothclient;

import java.util.HashMap;

import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.yadong.smarthome2.protocol2.Protocol;
import com.yadong.smarthome2.protocol2.ResultProtocol.Function;
import com.yadong.smarthome2.protocol2.ResultProtocol.Function.Argument;
import com.yadong.smarthome2.protocol2.Utils;
import com.yadong.smarthome2.protocol2.client.FunctionManager;
import com.yadong.smarthome2.protocol2.client.FunctionRunable;

public class FunctionManagerImpl extends FunctionManager {
    private View mRootView;
    private static final int MSG_LIGHT_ROOT = 1;

    public FunctionManagerImpl(MainActivity mainActivity) {
        mRootView = mainActivity.findViewById(R.id.root);
    }

    @Override
    protected HashMap<Function, FunctionRunable> createAllFunctions() {
        HashMap<Function, FunctionRunable> functions = 
                new HashMap<Function, FunctionRunable>();

        Function.Builder functionBuilder = Function.newBuilder();
        Argument.Builder argumentBuilder = Argument.newBuilder();

        //print function
        functionBuilder.setDescription("打印功能");
        functionBuilder.setCode("print_words");
        functionBuilder.addArguments(Utils.createArgument(argumentBuilder,
                "words", "S"));
        Function printFunctionFunction = functionBuilder.build();
        functions.put(printFunctionFunction, createPrintFunctionRunable(printFunctionFunction));

        //light function
        functionBuilder.clear();
        functionBuilder.setDescription("亮灯功能");
        functionBuilder.setCode("light_function");
        functionBuilder.addArguments(Utils.createArgument(argumentBuilder,
                "ligh", "B"));
        Function lightFunction = functionBuilder.build();
        functions.put(lightFunction, createLightFunction(lightFunction));
        return functions;
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MSG_LIGHT_ROOT:
                    boolean light = (Boolean) msg.obj;
                    if (light) {
                        mRootView.setBackgroundColor(0xffffffff);
                    } else {
                        mRootView.setBackgroundColor(0xff000000);
                    }
                    break;

                default:
                    break;
            }
        };
    };

    private FunctionRunable createLightFunction(final Function function) {
        return new FunctionRunable(function) {
            @Override
            public void run() {
                String lightOn = mRequest.getParameters(0).getArgs();
                Message message = mHandler.obtainMessage(MSG_LIGHT_ROOT);
                if (Protocol.BOOLEAN_TRUE.equals(lightOn)) {
                    message.obj = true;
                    message.sendToTarget();
                } else if (Protocol.BOOLEAN_FALSE.equals(lightOn)) {
                    message.obj = false;
                    message.sendToTarget();
                }
            }
        };
    }

    private FunctionRunable createPrintFunctionRunable(final Function function) {
        return new FunctionRunable(function) {
            @Override
            public void run() {
                String words = mRequest.getParameters(0).getArgs();
                System.out.println("yadong print .... " + words);
            }
        };
    }
}
